Ext.define('Module.Privilege.UserPrivilege', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.userprivilege',
	layout: 'border',
	initComponent : function() {
		var me = this;
		var eidVal=null;
		var dataVal=null;
		var tree =  Ext.create('Ext.tree.Panel',{
			region: 'west',
			title: '部门列表',
			rootVisible: true,
			width: '50%',
			padding: '0 1 0 0',
			columns:[{
				xtype: 'treecolumn', 
	            sortable: false,
	            flex:1,
	            dataIndex: 'name'
			}],
			store: Ext.create('Ext.data.TreeStore', {
				fields: ['id','text','name','type','code','addr','icon','zipcode','website','profile','updatetime','updateuser','iconCls','pid','eid'],
		        proxy: {
		            type: 'ajax',
		            url: 'enterprise/getEnterprise.do'
		        },
		        root: {
		        	iconCls:'icon-group',
		            id: '0',
		            name:'国网新疆乌鲁木齐供电公司',
		            expanded: true
		        }
		    }),
		    listeners: {
		    	select: function(model,rec){
		    	
		    		//usergrid.setTitle(rec.data.text + '---用户列表');
//	    			usergrid.store.proxy.extraParams.eid = rec.data.pid == 0 ? rec.data.id : rec.data.eid;
//	    			usergrid.store.proxy.extraParams.dept = rec.data.id;
	    			usergrid.store.load({
	    				params:{
	    					dept:rec.data.id == 0?null:rec.data.id,
	    					tj:null
	    				}
	    			});
	    			
	    			dataVal= rec.data.id;
	    			eidVal=rec.data.pid;
	    			
	    			menutree.setTitle('资源权限分配');
	    			menutree.store.proxy.extraParams.user= '';
	    			menutree.store.load();
	    			
	    			role.setTitle('用户角色');
	    			role.getSelectionModel().deselectAll(false);
	    			
	    			rmap.setTitle('视图效果');
	    			rmap.store.proxy.extraParams.user = -1;
	    			rmap.store.load();
		    	}
		    }
		});
		
		var usergrid = Ext.create('Ext.grid.Panel',{
			title: '部门用户列表',
			padding: '0 1 0 0',
			region: 'center',
			tbar: [{
				xtype: 'textfield',
				fieldLabel: '用户名',
				labelWidth:50,
				width: 180,
				maxLength:16,
				listeners: {
					change: function(f,v){
						usergrid.store.proxy.extraParams.tj = v;
					}
				}
			},{
				iconCls: 'icon-search',
				text: '查询',
				handler: function(){
					usergrid.store.load({params:{dept:null}});
//					if(eidVal == null && dataVal == null){
//						Ext.Msg.alert('提示', '请点击部门进行查询');
//					}else{
//						usergrid.store.load({params:{dept:null}});
//					}
//					usergrid.store.proxy.extraParams.dept
					
				}
			}],
			store: Ext.create('Ext.data.Store', {
				autoLoad: true,
				fields: ['id','account','name','password','eid','deptname','enable','createtime','updatetime','updateuser','updateusername'],
		        proxy: {
		            type: 'ajax',
		            url: 'user/getUser.do'
		        }
		    }),
		    columns: [{
		    	text: 'ID',
		    	dataIndex: 'id',
		    	width: 80,
		    	hidden: true
		    },{
		    	text: '用户帐户',
		    	dataIndex: 'account',
		    	flex: 1
		    },{
		    	text: '用户名',
		    	dataIndex: 'name',
		    	flex: 1
		    },{
		    	text: '所属部门',
		    	dataIndex: 'deptname',
		    	flex: 1,
		    	renderer:function(v){
		    		return v == ''?'国网新疆乌鲁木齐供电公司':v;
		    	}
		    }/*,{
		    	text: '创建时间',
		    	dataIndex: 'createtime',
		    	hidden: true,
		    	flex: 1.5,
		    	renderer: function(v){
		    		return Ext.Date.format(new Date(v),'Y-m-d h:m:s');
		    	}
		    },{
		    	text: '更新时间',
		    	dataIndex: 'updatetime',
		    	flex: 1.5,
		    	renderer: function(v){
		    		return Ext.Date.format(new Date(v),'Y-m-d h:m:s');
		    	}
		    },{
		    	text: '更新用户',
		    	hidden: true,
		    	dataIndex: 'updateusername',
		    	flex: 1
		    }*/],
		    listeners: {
		    	select: function(model,rec){
		    		menutree.store.proxy.extraParams.user=rec.data.id;
		    		rmap.store.proxy.extraParams.user=rec.data.id;
		    		menutree.store.load();
		    		rmap.store.load();
		    		
		    		role.setTitle('['+rec.data.name+']---角色分配');
		    		menutree.setTitle('['+rec.data.name+']---自定义资源');
		    		rmap.setTitle('['+rec.data.name+']---视图效果');
//		    		role.getSelectionModel().deselectAll(false);
		    		Ext.Ajax.request({
					    url: 'priv/user/getRole.do',
					    params: {
					    	user: rec.data.id
					    },
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	role.getSelectionModel().deselectAll(false);
        	    	    	for ( var i = 0; i < rs.length; i++) {
//								rs[i].setValue();
								role.getSelectionModel().select(role.store.findRecord('id',rs[i].role),true);
							}
        	    	    }
					});
		    	}
		    }
		});
		
		var menutree =  Ext.create('Ext.tree.Panel',{
			rootVisible: false,
//			border: false,
			title: '自定义资源',
			padding: '0 1 0 0',
			width: '30%',
			region: 'center',
			widht: 200,
			store: Ext.create('Ext.data.TreeStore', {
				fields: ['id','index'/*,'leaf'*/,'link','pid','text','linktype',{name: 'checked',type: 'boolean'}],
		        proxy: {
		        	extraParams: {user: ''},
		            type: 'ajax',
		            url: 'priv/user/getMenu.do'
		        },
		        root: {
		            id: '0'
		        },
		        listeners: {
		        	load: function(){
//		        		menutree.expandAll(function(){
//			    			menutree.collapseAll();
//			    		});
		        	}
		        }
		    }),
		    tbar: [{
		    	text: '保存权限',
		    	iconCls: 'icon-save',
		    	handler: function(){
		    		var userid = menutree.store.proxy.extraParams.user;
		    		if(userid && '' != userid){
		    			var checkeds = [];
			    		Ext.each(menutree.getChecked(), function(node) {
			    			checkeds.push(node.data.id);
			    		});
		    			Ext.Ajax.request({
						    url: 'priv/user/addPriv.do',
						    params: {
						    	user: userid,
						        ids: checkeds.join(',')
						    },
						    success: function(response){
	        	    	    	var rs = Ext.decode(response.responseText);
	        	    	    	if(rs.rst){
	        	    	    		Ext.Msg.alert('提示', '保存成功！');
	        	    	    		rmap.store.load();
	        	    	    	}
	        	    	    	else{
	        	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
	        	    	    	}
	        	    	    }
						});
		    		}
		    		else{
		    			Ext.Msg.alert('提示', '保存失败，没有要保存的用户权限，请选择用户。');
		    		}
		    	}
		    }/*,{
		    	text: '展开所有',
		    	iconCls: 'icon-foward',
		    	handler: function(){
		    		menutree.expandAll();
		    	}
		    },{
		    	text: '折叠所有',
		    	iconCls: 'icon-backward',
		    	handler: function(){
		    		menutree.collapseAll();
		    	}
		    }*/,{
		    	text: '全选',
		    	iconCls: 'icon-action_check',
		    	handler: function(){
		    		menutree.getRootNode().cascadeBy(function(n){
	    				n.set('checked',true);
	    			});
		    	}
		    },{
		    	text: '全不选',
		    	iconCls: 'icon-stopplay',
		    	handler: function(){
		    		menutree.getRootNode().cascadeBy(function(n){
	    				n.set('checked',false);
	    			});
		    	}
		    },{
		    	text: '刷新',
		    	iconCls: 'icon-arrow_refresh',
		    	handler: function(){
		    		menutree.store.load();
		    	}
		    }],
		    listeners: {
		    	checkchange: function(node,checked){
		    		if(checked){
		    			checkAllParent(node);
		    			node.cascadeBy(function(n){
		    				n.set('checked',true);
		    			});
		    			if(node.parentNode){
		    				node.parentNode.set('checked',true);
		    			}
		    		}
		    		else if(!checked){
		    			node.cascadeBy(function(n){
		    				n.set('checked',false);
		    			});
		    		}
		    	}
		    }
		});
		
		var role = Ext.create('Ext.grid.Panel',{
			title: '角色分配',
			region: 'west',
			width: '50%',
			region: 'east',
			selModel: Ext.create('Ext.selection.CheckboxModel'),
			tbar: [{
		    	text: '保存角色',
		    	iconCls: 'icon-save',
		    	handler: function(){
		    		var userid = menutree.store.proxy.extraParams.user;
		    		if(userid == 0){
		    			Ext.Msg.alert('提示', '系统默认用户,已拥有所以权限。');
		    			return;
		    		}
		    		if(userid && '' != userid){
		    			var checkeds = [];
			    		Ext.each(role.getSelectionModel().getSelection(), function(rec) {
			    			checkeds.push(rec.data.id);
			    		});
		    			Ext.Ajax.request({
						    url: 'priv/user/addPrivRole.do',
						    params: {
						    	user: userid,
						    	roles: checkeds.join(',')
						    },
						    success: function(response){
	        	    	    	var rs = Ext.decode(response.responseText);
	        	    	    	if(rs.rst){
	        	    	    		Ext.Msg.alert('提示', '保存成功！');
	        	    	    		rmap.store.load();
	        	    	    	}
	        	    	    	else{
	        	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
	        	    	    	}
	        	    	    }
						});
		    		}
		    		else{
		    			Ext.Msg.alert('提示', '保存失败，没有要保存的用户权限，请选择用户。');
		    		}
		    	}
		    }],
			store: Ext.create('Ext.data.Store', {
				autoLoad: true,
				fields: ['id','name','describe','enbale','createtime','updatetime','updateuser'],
		        proxy: {
		            type: 'ajax',
		            reader: 'json',
		            url: 'priv/role/getRole.do'
		        }
		    }),
		    columns: [{
		    	text: 'ID',
		    	dataIndex: 'id',
		    	width: 80
		    },{
		    	text: '角色名称',
		    	dataIndex: 'name',
		    	flex: 1
		    },{
		    	text: '角色描述',
		    	dataIndex: 'describe',
		    	flex: 1,
		    	renderer: function(v){
		    		return '<span title="'+v+'">'+v+'</span>';
		    	}
		    }]
		});
		
		
		
		var rmap =  Ext.create('Ext.tree.Panel',{
			rootVisible: false,
			title: '视图效果',
			region: 'east',
			padding: '0 1 0 0',
			collapsed: true,
			collapsible: true,
			width: '30%',
			tbar: [{
		    	text: '展开所有',
		    	iconCls: 'icon-foward',
		    	handler: function(){
		    		rmap.expandAll();
		    	}
		    },{
		    	text: '折叠所有',
		    	iconCls: 'icon-backward',
		    	handler: function(){
		    		rmap.collapseAll();
		    	}
		    },{
		    	text: '刷新',
		    	iconCls: 'icon-arrow_refresh',
		    	handler: function(){
		    		rmap.store.load();
		    	}
		    }],
			store: Ext.create('Ext.data.TreeStore', {
		        proxy: {
		            type: 'ajax',
		            extraParams: {user: -1},
		            url: 'menu/getPrivMenu.do'
		        },
		        root: {
		            id: '0'
		        }
		    })
		});
		
		
		me.items = [{
			region: 'west',
//			title: 'a',
			border : false,
			width: '50%',
			layout: 'border',
			items: [tree,usergrid]
		},{
			region: 'center',
			border : false,
//			title: 'B',
			layout: 'border',
			xtype: 'tabpanel',
			items: [role/*,menutree*/]
		}/*,rmap*/];
		
		me.callParent();
	}
});
